package Thread;

import java.util.concurrent.atomic.AtomicInteger;

//原子类避免线程安全问题。原子类是基于CAS实现的
public class Demo25 {
    private static AtomicInteger count = new AtomicInteger(0);
    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Thread(()->{
            for (int i = 0; i < 5000; i++) {
                //相当于count++
                count.getAndIncrement();
            }
        });
        Thread t2 = new Thread(()->{
            for (int i = 0; i < 5000; i++) {
                count.getAndIncrement();
            }
        });
        t1.start();
        t2.start();
        Thread.sleep(1000);
//        t1.join();
//        t2.join();
        System.out.println(count.get());
    }
}
